<?php

class Default_MobileController extends Ac_Controller_Mobile {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        $this->_helper->layout->setLayout('layout-mobile2');
        //$this->view->main_content = "mobile/homepage.phtml";
        //$this->view->module_content = "homepage";
        //today's pick
        //2011-03-04
        if ($this->_request->getParam('module') != 'default') {
            throw new Exception('wrong module');
        }
        $this->view->categorydata = Model_Category::getLevel1(array('homepage' => 1, 'status' => Model_Category::STATUS_ACTIVE));
        /*$arr = array(
            new MongoId('4d86f78db7ca870e34125ce6'),
            new MongoId('4d807c79b7ca870e340f1e6f'),
            new MongoId('4d7f5f6cb7ca870e340ea9d2'),
            new MongoId('4d7dca01b7ca870e340dd110'),
            new MongoId('4d7da51eb7ca870e340dbae9'),
            new MongoId('4d78a052b7ca870e340b8c81'),
            new MongoId('4d8188c6b7ca870e340fbaae'),
            new MongoId('4d84b3d0b7ca870e34113acd'),
            new MongoId('4d774adeb7ca870e340aef57'),
            new MongoId('4d75fc0ab7ca870e340a5eb3'),
            new MongoId('4d759f2db7ca870e340a31eb'),
            new MongoId('4d74a4a7b7ca870e3409badc'),
            new MongoId('4d747b8eb7ca870e3409a382'),
            new MongoId('4d730e03b7ca870e3408e3fa'),
            new MongoId('4d871b8fb7ca870e34127600'),
            new MongoId('4d8700f6b7ca870e341263c2'),
            new MongoId('4d86f974b7ca870e34125e33'),
        );*/
	$arr = array(
	new MongoId('4da9b97fdcab9f1a7cb8b7f9'),
                new MongoId('4da7ca2ddcab9f1a7cb6b4c2'),
                new MongoId('4da69332dcab9f1a7cb5d504'),  
                new MongoId('4daee475dcab9f1a7cbcb67f'),
                new MongoId('4dada134dcab9f1a7cbb3d12'),
                new MongoId('4dad9a2edcab9f1a7cbb31ff'),
                new MongoId('4dad9035dcab9f1a7cbb224c'),
                new MongoId('4d6f59abb7ca870e34075db4'),
                new MongoId('4dad5230dcab9f1a7cbae6bd'),
                new MongoId('4d9e9f10dcab9f1a7caff3f6'),
                new MongoId('4dac2e44dcab9f1a7cba3c1f'),
                new MongoId('4d9173a9b7ca870e34186bff'),
                new MongoId('4d8dd8cab7ca870e341608fe'),
                new MongoId('4da95c82dcab9f1a7cb878ed'),
                new MongoId('4da95a5cdcab9f1a7cb8752a'),
                new MongoId('4da5ada4dcab9f1a7cb57a2a'),
                new MongoId('4d8706f0b7ca870e3412675b'),
                new MongoId('4d76e43fb7ca870e340abbd6'),
                new MongoId('4d76ea50b7ca870e340abe3d'),
                new MongoId('4da564e3dcab9f1a7cb554ac'),
	);

        /* shuffle($arr);
          $arr = array_slice($arr, 0, 6);

          //$res = Model_Listing::all(array('_id' => array('$in' => $arr)));
          $list = array();
          foreach($arr as $val) {
          $list[] = Model_Listing::one($val);
          } */
        shuffle($arr);
        $arr = array_slice($arr, 0, 6);
        //$res = Model_Listing::all(array('_id' => array('$in' => $arr)));
        $list = array();
        foreach ($arr as $val) {
            $tmp = Model_Listing::find($val);
            if (!empty($tmp)) {
                $list[] = $tmp;
            }
        }
        $this->view->todayspick = $list;
        //$this->_forward('index', 'mobile', 'homepage');
    }

    public function bookmarkAction() {
        if ($this->_request->isPost() && $this->_request->isXmlHttpRequest()) {
            if (empty($this->loggedin_user)) {
                $mobilespace = new Zend_Session_Namespace('Mobile');
                $mobilespace->bookmarkID = $_POST['data'];
                $this->_helper->json(array('res' => 'redirect'));
            }
            if (Model_Bookmark::addListing($_POST['data'])) {
                $this->_helper->json(array('res' => 'ok'));
            }
        }
        exit;
    }

    public function bookmark2Action() {
        if ($this->_request->isPost() && $this->_request->isXmlHttpRequest()) {
            if (empty($this->loggedin_user)) {
                $mobilespace = new Zend_Session_Namespace('Mobile');
                $mobilespace->removeBookmarkiID = $_POST['data'];
                $this->_helper->json(array('res' => 'redirect'));
            }

            $user_id = Zend_Registry::get('loggedin_user')->_id;
            if (Model_Bookmark::remove(array('ls' => $_POST['data'], 'us' => $user_id))) {
                $this->_helper->json(array('res' => 'ok'));
            }
        }
        exit;
    }

    public function bookmark3Action() {
        if ($this->_request->isPost() && $this->_request->isXmlHttpRequest()) {
            $user_id = Zend_Registry::get('loggedin_user')->_id;
            if (Model_Bookmark::remove(array('us' => $user_id))) {
                echo 'ok';
            }
        }
        exit;
    }

    /* public function removehistoryAction()
      {
      if ($this->_request->isPost() && $this->_request->isXmlHttpRequest()) {
      $uniqid = $_COOKIE['ac_bookmark'];
      if(!empty($this->loggedin_user))
      {
      $user_id = Zend_Registry::get('loggedin_user')->_id;
      if(Model_History::remove(array('ls'=>new MongoID($_POST['data']), 'uid'=>$uniqid)))
      {
      echo 'ok1';
      }
      }
      else
      {
      if(Model_History::remove(array('ls'=>new MongoID($_POST['data']), 'uid'=>$uniqid)))
      {
      echo 'ok';
      }
      }
      }
      exit;
      }
      public function removehistoryAction()
      {
      if (!empty($_GET['hid'])) {
      $uniqid = $_COOKIE['ac_bookmark'];
      if(Model_History::remove(array('ls'=>new MongoID($_GET['hid']), 'uid'=>$uniqid)))
      {
      $temp = true;
      }
      }
      $this->_redirect($this->view->geturl('viewed', 'user', null, null, !empty($_GET['page'])?array('page' => $_GET['page']):array()));
      } */

    public function removehistory2Action() {
        if ($this->_request->isPost() && $this->_request->isXmlHttpRequest()) {
            $uniqid = $_COOKIE['ac_bookmark'];

            if (Model_History::remove(array('uid' => $uniqid))) {
                echo 'ok';
            }
        }
        exit;
    }

    public function privacyAction() {
        $this->_helper->layout->setLayout('layout-mobile2');
        $this->render('privacy');
    }

    public function termOfUseAction() {
        $this->_helper->layout->setLayout('layout-mobile2');
        $this->render('term-of-use');
    }

    public function backAction() {
        $mobilespace = new Zend_Session_Namespace('Mobile');
        $url = $mobilespace->back_url;
        $back_url = $url[(count($url) - 2)];
        unset($url[(count($url) - 1)]);
        unset($url[(count($url) - 1)]);
        $mobilespace->back_url = $url;
        $this->_redirect($back_url);
    }

    public function mobileAction() {
        $this->_redirect('/');
    }

}
